package cn.gzjp.modules.logis.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import cn.gzjp.common.utils.CacheUtils;
import cn.gzjp.common.utils.MyBeanUtils;
import cn.gzjp.common.web.BaseController;
import cn.gzjp.common.utils.StringUtils;
import cn.gzjp.modules.logis.entity.NewsType;
import cn.gzjp.modules.logis.service.NewsTypeService;

/**
 * 新闻类型Controller
 * @author zengcp
 * @version 2017-08-17
 */
@Controller
@RequestMapping(value = "${adminPath}/portal/newsType")
public class NewsTypeController extends BaseController {

	@Autowired
	private NewsTypeService newsTypeService;
	
	
	/**
	 * 新闻类型列表页面
	 */
	@RequiresPermissions("portal:newsType:list")
	@RequestMapping(value = {"list", ""})
	public String list(NewsType newsType, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<NewsType> list = newsTypeService.findList(newsType);
		model.addAttribute("list", list);
		return "modules/logis/newsTypeList";
	}

	/**
	 * 查看，增加，编辑新闻类型表单页面
	 */
	@RequiresPermissions(value={"portal:newsType:view","portal:newsType:add","portal:newsType:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(NewsType newsType, Model model) {
		if (newsType.getParent()!=null && StringUtils.isNotBlank(newsType.getParent().getId())){
			newsType.setParent(newsTypeService.get(newsType.getParent().getId()));
			// 获取排序号，最末节点排序号+30
			if (StringUtils.isBlank(newsType.getId())){
				NewsType search = new NewsType();
				search.setParent(new NewsType(newsType.getParent().getId()));
				List<NewsType> list = newsTypeService.findList(search); 
				if (list.size() > 0){
					newsType.setSort(list.get(list.size()-1).getSort());
					if (newsType.getSort() != null){
						newsType.setSort(newsType.getSort() + 30);
					}
				}
			}
		}
		if (newsType.getSort() == null){
			newsType.setSort(30);
		}
		model.addAttribute("newsType", newsType);
		return "modules/logis/newsTypeForm";
	}

	/**
	 * 保存新闻类型
	 */
	@RequiresPermissions(value={"portal:newsType:add","portal:newsType:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(NewsType newsType, Model model, RedirectAttributes redirectAttributes) throws Exception{
//		if(newsType.getParentId()!=null&&!newsType.getParentId().equals("0")&&!"9bc22fbd5e1b4842b91f9c6193b9c8b8".equals(newsType.getParentId())&&!"67d6759c44cc4a53b5eb19e6254f6e91".equals(newsType.getParentId())){
//			addMessage(redirectAttributes, "上级选择有误，只支持二级，保存新闻类型失败");
//			return redirect("/portal/newsType/?repage");
//		}
		if (!beanValidator(model, newsType)){
			return form(newsType, model);
		}
		if(!newsType.getIsNewRecord()){//编辑表单保存
			NewsType t = newsTypeService.get(newsType.getId());//从数据库取出记录的值
			MyBeanUtils.copyBeanNotNull2Bean(newsType, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
			newsTypeService.save(t);//保存
		}else{//新增表单保存
			newsTypeService.save(newsType);//保存
		}
		CacheUtils.remove("newsTypes");
		addMessage(redirectAttributes, "保存成功");
		return redirect("/portal/newsType/?repage");
	}
	
	/**
	 * 删除新闻类型
	 */
	@RequiresPermissions("portal:newsType:del")
	@RequestMapping(value = "delete")
	public String delete(NewsType newsType, RedirectAttributes redirectAttributes) {
		newsTypeService.delete(newsType);
		CacheUtils.remove("newsTypes");
		addMessage(redirectAttributes, "删除成功");
		return redirect("/portal/newsType/?repage");
	}
	
	/**
	 * 批量删除新闻类型
	 */
	@RequiresPermissions("portal:newsType:del")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			newsTypeService.delete(newsTypeService.get(id));
		}
		CacheUtils.remove("newsTypes");
		addMessage(redirectAttributes, "删除成功");
		return redirect("/portal/newsType/?repage");
	}
	
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<NewsType> list = newsTypeService.findList(new NewsType());
		for (int i=0; i<list.size(); i++){
			NewsType e = list.get(i);
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("name", e.getName());
				mapList.add(map);
		}
		return mapList;
	}

	@ModelAttribute
	public NewsType get(@RequestParam(required=false) String id) {
		NewsType entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = newsTypeService.get(id);
		}
		if (entity == null){
			entity = new NewsType();
		}
		return entity;
	}
	

}